package com.xc.ksxt.controller;

import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.Choice;
import com.xc.ksxt.entity.ChoiceEntity;
import com.xc.ksxt.service.ChoiceService;
import com.xc.ksxt.vo.ChoiceVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/choice")
public class ChoiceController {

    @Autowired
    private ChoiceService choiceService;
    private Choice CHOICE = Choice.CHOICE.as("CHOICE");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<ChoiceVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<ChoiceVO> ltr = new LayuiTableResponse<>();
        Condition cond = CHOICE.ID.notEqual(-1l);
        String questionId = request.getParameter("questionId");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(questionId)) {
            cond = cond.and(CHOICE.QUESTION_ID.eq(Long.valueOf(questionId)));
        }
        Long count = choiceService.count(cond);
        ltr.setCount(count);
        PageInfo<ChoiceEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = choiceService.select(cond, pageInfo,CHOICE.INDEX_LETTER.asc(),CHOICE.CREATE_TIME.desc());
        List<ChoiceVO> choiceEntityList = list.into(ChoiceVO.class);
        for (ChoiceVO choiceEntity : choiceEntityList){
        }
        ltr.setData(choiceEntityList);
        return ltr;
    }
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getById"})
    public ChoiceEntity getById(Long id) {
        ChoiceEntity entity = choiceService.getEntityById(id);
        return entity;
    }
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.POST})
    @RequestMapping({"/save"})
    public ResponseEntity<ChoiceEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<ChoiceEntity> res = new ResponseEntity<>();
        ChoiceEntity choiceEntity = Helper.map2Bean(new ChoiceEntity(), params);
        choiceService.save(choiceEntity);
        return res;
    }
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.POST})
    @RequestMapping({"/delete"})
    public LayuiTableResponse<ChoiceEntity> delete(@RequestBody Map<String,String> params) {
        String ids = params.get("ids");
        Long[] idarr = Helper.str2longArr(ids);
        LayuiTableResponse<ChoiceEntity> ltr = new LayuiTableResponse<>();
        for (Long id : idarr) {
            ChoiceEntity m = choiceService.getEntityById(id);
            choiceService.update(m);
        }
        return ltr;
    }

}
